const ACCESS_TOKEN_KEY = "ACCESS_TOKEN";

$(() => {

    /**
     * 授权码接口
     */
    const authorization = () => {
        let authorizationCode = window.prompt("请输入授权码") || "";
        if(authorizationCode){
            $.ajax({
                url: '/authorization?code=' + authorizationCode,
                type: 'get',
                contentType: 'application/json',
                dataType: 'json',
                success: (res) => {
                    if (res.code == 200) {
                        window.sessionStorage.setItem(ACCESS_TOKEN_KEY, res.data);
                        fetchFileList("/");
                    }else{
                        authorization();
                    }
                }
            });
        }
    };

    //目录检索
    const fetchFileList = (targetPath) => {
        $("#target-path").text(targetPath);
        $.ajax({
            url: '/file',
            type: 'post',
            data: JSON.stringify({
                dir: targetPath
            }),
            contentType: 'application/json',
            dataType: 'json',
            success: (res) => {
                let list = res.data;
                let htmlStr = ``;
                if(targetPath != '/'){
                    htmlStr += `<li>
                        <div class="file-name">../</div>
                        <div class="file-type"></div>
                        <div class="file-size"></div>
                        <div class="file-link"></div>
                    </li>`;
                }
                for (const item of list) {
                    htmlStr += `<li data-fsid="${item.fsId}" data-isdir="${item.isdir}" title="${item.path}">
                        <div class="file-name">${item.serverFilename}</div>
                        <div class="file-type">${item.isdir == '1' ? '目录' : '文件'}</div>
                        <div class="file-size">${item.isdir == '1' ? '' : (item.size/1024/1024).toFixed(2) + 'MB'}</div>
                        <div class="file-link">
                            <img src="/img/download.png" />
                        </div>
                    </li>`;
                }
                $("#file-list").html(htmlStr);
            }
        });
    };

    /**
     * 双击打开目录
     */
    $("#file-list").on("dblclick","li",(e) => {
        let path = $(e.target).parent("li").attr("title");
        if(path){
            let isdir = $(e.target).parent("li").data("isdir");
            if(isdir == '1'){
                fetchFileList(path);
            }
        }else{
            path = $("#target-path").text();
            path = path.substring(0,path.lastIndexOf("/"));
            fetchFileList(path == "" ? "/" : path);
        }
    });

    /**
     * 点击下载
     */
    $("#file-list").on("click","img",(e) => {
        console.log($(e.target).parents("li"))
        let fsid = $(e.target).parents("li").data("fsid");
        if(fsid){
            window.open("/file/download?fsid=" + fsid);
        }
    });

    //授权
    let accessToken = window.sessionStorage.getItem(ACCESS_TOKEN_KEY);
    if (accessToken) {
        fetchFileList("/");
    }else{
        authorization();
    }

})